package com.psycho.sys.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.psycho.sys.api.entity.Appointment;
import com.psycho.sys.api.entity.Counselor;
import com.psycho.sys.api.entity.ConsultationRecord;
import com.psycho.sys.api.mapper.AppointmentMapper;
import com.psycho.sys.api.mapper.CounselorMapper;
import com.psycho.sys.api.mapper.ConsultationRecordMapper;
import com.psycho.sys.api.service.CounselorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 咨询师服务实现类
 */
@Service
public class CounselorServiceImpl extends ServiceImpl<CounselorMapper, Counselor> implements CounselorService {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @Autowired
    private ConsultationRecordMapper consultationRecordMapper;

    @Override
    public Counselor getCounselorProfile(Long counselorId) {
        return this.getById(counselorId);
    }

    @Override
    public boolean updateCounselorProfile(Counselor counselor) {
        return this.updateById(counselor);
    }

    @Override
    public List<Appointment> getPendingAppointments(Long counselorId) {
        LambdaQueryWrapper<Appointment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Appointment::getCounselorId, counselorId)
                .eq(Appointment::getStatus, 0)
                .orderByAsc(Appointment::getAppointmentTime);
        return appointmentMapper.selectList(wrapper);
    }

    @Override
    @Transactional
    public boolean updateAppointmentStatus(Long appointmentId, Integer status, String reason) {
        Appointment appointment = appointmentMapper.selectById(appointmentId);
        if (appointment == null) {
            return false;
        }
        appointment.setStatus(status);
        appointment.setCancelReason(reason);
        return appointmentMapper.updateById(appointment) > 0;
    }

    @Override
    @Transactional
    public boolean startConsultation(Long appointmentId) {
        Appointment appointment = appointmentMapper.selectById(appointmentId);
        if (appointment == null || appointment.getStatus() != 1) {
            return false;
        }

        ConsultationRecord record = new ConsultationRecord();
        record.setAppointmentId(appointmentId);
        record.setUserId(appointment.getUserId());
        record.setCounselorId(appointment.getCounselorId());
        record.setStartTime(LocalDateTime.now());
        record.setStatus(0);

        return consultationRecordMapper.insert(record) > 0;
    }

    @Override
    public boolean recordConsultation(ConsultationRecord record) {
        // 设置更新时间
        record.setUpdateTime(LocalDateTime.now());

        if (record.getId() != null) {
            // 如果提供了ID，说明是更新操作
            return consultationRecordMapper.updateById(record) > 0;
        } else {
            // 如果没有提供ID，说明是新增操作
            record.setCreateTime(LocalDateTime.now());
            // 检查预约是否存在
            Appointment appointment = appointmentMapper.selectById(record.getAppointmentId());
            if (appointment == null) {
                return false;
            }
            // 设置关联信息
            if (record.getUserId() == null) {
                record.setUserId(appointment.getUserId());
            }
            if (record.getCounselorId() == null) {
                record.setCounselorId(appointment.getCounselorId());
            }
            return consultationRecordMapper.insert(record) > 0;
        }
    }

    @Override
    public List<ConsultationRecord> getConsultationRecords(Long counselorId) {
        LambdaQueryWrapper<ConsultationRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ConsultationRecord::getCounselorId, counselorId)
                .orderByDesc(ConsultationRecord::getCreateTime);
        return consultationRecordMapper.selectList(wrapper);
    }

    @Override
    public boolean updateConsultationRecord(ConsultationRecord record) {
        record.setUpdateTime(LocalDateTime.now());
        return consultationRecordMapper.updateById(record) > 0;
    }

    @Override
    public List<ConsultationRecord> getStudentFeedback(Long counselorId) {
        LambdaQueryWrapper<ConsultationRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ConsultationRecord::getCounselorId, counselorId)
                .isNotNull(ConsultationRecord::getUserFeedback)
                .orderByDesc(ConsultationRecord::getCreateTime);
        return consultationRecordMapper.selectList(wrapper);
    }

    @Override
    public boolean replyToFeedback(Long recordId, String counselorFeedback) {
        ConsultationRecord record = consultationRecordMapper.selectById(recordId);
        if (record == null) {
            return false;
        }
        record.setCounselorFeedback(counselorFeedback);
        record.setUpdateTime(LocalDateTime.now());
        return consultationRecordMapper.updateById(record) > 0;
    }
}